package org.grottarossa.ui.core.casemanagement.contents;




import java.math.BigDecimal;
import java.util.ResourceBundle;

import org.grottarossa.template.data.pojo.content.Content;
import org.grottarossa.template.data.pojo.content.ContentList;
import org.grottarossa.template.drivers.DriverException;
import org.grottarossa.template.drivers.components.tokens.CaseManagementId;
import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.base.BasePanel;
import org.grottarossa.ui.core.base.IBaseComponent;
import org.grottarossa.ui.core.casemanagement.CaseTabSheet;
import org.grottarossa.ui.core.container.ContentContainer;
import org.grottarossa.ui.core.controls.document.DocumentVersionsManager;
import org.grottarossa.ui.core.controls.document.DocumentVersionsManagerFactory;
import org.grottarossa.ui.core.i18n.DM;
import org.grottarossa.ui.core.layout.EmptyPanel;
import org.grottarossa.ui.core.layout.LayoutFactory;

import com.vaadin.data.Container;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TreeTable;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;

/***
 * Pannello della lista dei documenti del CaseFolder
 * @author 
 *
 */
public class ContentListPanel extends BasePanel implements ItemClickListener
{
	private static final long serialVersionUID = 5367036209492343058L;
	ContentList contentList = null;
	private final CaseTabSheet m_caseTabSheet;
	private TreeTable m_treeTable;
	private CaseManagementId m_id;

	public ContentListPanel(ApplicationSession appSession, CaseManagementId id, CaseTabSheet caseTabSheet)
	{
		super(appSession, DM.instance().getBundle(ContentListPanel.class.getName()));

		this.m_id = id;
		this.m_caseTabSheet = caseTabSheet;
		initializeComponents() ;
	}

	@Override
	protected void initializeComponents()
	{
		VerticalLayout v = new VerticalLayout();
		v.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		v.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		setLayout(v);

		m_treeTable = new TreeTable();
		m_treeTable.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		m_treeTable.setWidth(100, Sizeable.UNITS_PERCENTAGE);

		ContentContainer container = reload(m_id);

		m_treeTable.setCacheRate(100);
		m_treeTable.setColumnCollapsingAllowed(true);
		m_treeTable.setPageLength( container.getItemIds().size());
		m_treeTable.setSortDisabled(true);

		for (Object obj : container.getItemIds() )
		{
			m_treeTable.setCollapsed(obj, false);
		}

		m_treeTable.addListener(this);
		v.addComponent(m_treeTable);
	}

	/***
	 * Esegue il refresh della lista
	 */
	public void refresh()
	{
		reload(m_id);
	}

	/***
	 * Ricarica la lista leggendo i dati da un case folder specificato
	 * @param id Id del case folder
	 * @return
	 */
	private ContentContainer reload(CaseManagementId id)
	{
		try
		{
			contentList = m_appSession.getBuilder().getDocumentManagement().getContentsFromCaseFolder( id );
		}
		catch (DriverException e)
		{
			showError(e);
		}

		ContentContainer container = new ContentContainer(contentList);
		m_treeTable.setContainerDataSource(container);
		m_treeTable.setVisibleColumns(container.getVisibleColumns());
		return container;
	}

	/***
	 * Gestione delll'evento di click su un elemento della lista
	 */
	public void itemClick(ItemClickEvent event)
	{
		Integer idx = (Integer)event.getItem().getItemProperty("contentID").getValue();
		Content content = contentList.get(idx);

		if(content.isSection())
			return;

		try
		{
			ContentPanel cp = new ContentPanel(m_appSession, content);
			showInPopup(cp, 640, 480, m_localizer.getString("popup.caption.contentdetail"), true, true);
		}
		catch(Exception ex)
		{
			showError(ex);
		}
	}
}
